<template>
  <div style="height: 100%">
    <div style="width: 49%;float: left;height: 100%;">
      <el-card style="border-radius: 10px;height: 100%">
        <p style="text-align: center;font-size: 35px;font-weight: 500">我 的 信 息</p>
        <p style="text-align: center;font-size: 35px;font-weight: 300;margin-top: 100px">用 户 名： {{info.username}}</p>
        <p style="text-align: center;font-size: 35px;font-weight: 300;margin-top: 30px">邮 箱： {{info.email}}</p>
      </el-card>
    </div>
    <div style="width: 49%;float: right;height: 100%;">
      <el-card style="border-radius: 10px;height: 100%">
        <p style="text-align: center;font-size: 35px;font-weight: 500">修 改 密 码</p>
        <el-form>
          <el-form-item>
            <el-input type="password" v-model="form.old" placeholder="请输入旧密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input type="password" v-model="form.password" placeholder="请输入新密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input type="password" v-model="form.double" placeholder="请再输入一次"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="success" plain style="width: 100%" @click="changePass">确 定</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  name: "MyInformation",
  data(){
    return{
      info:{},
      form:{
        uid:"",
        password:"",
        double:"",
        old:""
      }
    }
  },
  created() {
    this.getUserInfo();
  },
  methods:{
    async getUserInfo(){
      let form = {
        uid:window.sessionStorage.getItem("userId")
      }
      const{data:res} = await this.$http.post('http://120.46.132.13:8081/getUserInfo',form);
      if(res.code===200||res.code===0){
        this.info = res.data;
      }else{
        this.$notify.error("获取用户信息失败")
      }
    },
    async changePass(){
      this.form.uid = window.sessionStorage.getItem("userId")
      if(this.$md5(this.form.old)!==window.sessionStorage.getItem("pass")){
        this.$notify.error("旧密码错误")
        return
      }
      if(this.form.password.length<6){
        this.$notify.error("密码长度应大于5")
        return
      }
      if(this.form.password!==this.form.double){
        this.$notify.error("两次密码输入不一致")
        return
      }
      this.form.password = this.$md5(this.form.password)
      const {data:res} = await this.$http.post('http://120.46.132.13:8081/changePassword',this.form)
      if(res.code===200||res.code===0){
        window.sessionStorage.setItem("pass",this.$md5(this.form.password))
        this.$notify.success("修改成功")
      }else{
        this.$notify.error("修改失败")
      }

      this.form.double="";
      this.form.password="";
      this.form.old="";
    }
  }
}
</script>

<style scoped>

</style>
